//
//  ABViewCell.m
//  聚超值
//
//  Created by 周abel on 16/3/31.
//  Copyright © 2016年 Zhli. All rights reserved.
//

#import "ABViewCell.h"
#import "ABItem.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "XMGInfiniteScrollView.h"

#define imageVW self.scrollView.bounds.size.width
#define imageVH self.scrollView.bounds.size.height

@interface ABViewCell () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

// 保存视图,循环利用
@property (nonatomic, strong) UIImageView *centerImageView;
@property (nonatomic, strong) UIImageView *reusedImageView;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, assign) NSInteger imageCount;

/** 定时器接口*/
@property (nonatomic, strong) NSTimer *timer;

// 保存图片
@property (nonatomic, strong)NSMutableArray *pictureArr;

@property (weak, nonatomic) IBOutlet UIPageControl *pageView;


@end
@implementation ABViewCell

- (NSMutableArray *)pictureArr {

    if (_pictureArr == nil) {
        _pictureArr = [NSMutableArray array];
    }
    return _pictureArr;
}

#pragma mark -从网络获取图片
- (void)setItemArr:(NSArray *)itemArr {

    // 每次刷新,清除旧数据
    self.pictureArr = nil;
    self.pageView.currentPage = 0;
    
    _itemArr = itemArr;

    for (ABItem *item in itemArr) {
        NSURL *imageUrl = [NSURL URLWithString:item.imageUrl];
        [self.pictureArr addObject:imageUrl];
    }
    
    // 显示图片
    [self.centerImageView sd_setImageWithURL:self.pictureArr[0] placeholderImage:nil];

    [self.reusedImageView sd_setImageWithURL:self.pictureArr[1] placeholderImage:nil];
    
    self.imageCount = 1;
    
    // 开启定时器
    [self startTimer];
}

//添加图片轮播器方法
-(void)setUpPigeView
{
    //添加图片轮播器
    XMGInfiniteScrollView *scrollView = [[XMGInfiniteScrollView alloc] init];
    scrollView.frame = self.scrollView.bounds;
    scrollView.images = @[
                          [UIImage imageNamed:@"img_00"],
                          [UIImage imageNamed:@"img_01"],
                          [UIImage imageNamed:@"img_02"],
                          [UIImage imageNamed:@"img_03"],
                          [UIImage imageNamed:@"img_04"]
                          ];
    scrollView.pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    scrollView.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    //    scrollView.scrollDirectionPortrait = YES;
    [self.scrollView addSubview:scrollView];
}

#pragma mark -循环利用图片
- (void)awakeFromNib {
    
    
    [self setUpPigeView];
    
//    // 设置scrollView
//    self.scrollView.bounces = NO;
//    self.scrollView.pagingEnabled = YES;
//    self.scrollView.showsVerticalScrollIndicator = NO;
//    self.scrollView.showsHorizontalScrollIndicator = NO;
//    self.scrollView.delegate = self;
//    
//    //1.0 设置scrollView的内容尺寸
//    self.scrollView.contentSize = CGSizeMake(2*imageVW, imageVH);
//    self.scrollView.pagingEnabled = YES;
//    
//    // 2.0 添加imagview
//    UIImageView *centerImageV = [[UIImageView alloc] init];
//    centerImageV.frame = CGRectMake(0, 0, imageVW, imageVH);
//    [self.scrollView addSubview:centerImageV];
//    self.centerImageView = centerImageV;
//    
//    UIImageView *reusedImageV = [[UIImageView alloc] init];
//    reusedImageV.frame = CGRectMake(imageVW, 0, imageVW, imageVH);
//    [self.scrollView addSubview:reusedImageV];
//    self.reusedImageView = reusedImageV;
//    
//    // 设置scrollView的代理
//    self.scrollView.delegate = self;
//    
//    // 监听通知
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scroll) name:@"scroll" object:nil];
//    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopScroll) name:@"stop" object:nil];
//    
//    // 允许图片进行交互
//    self.centerImageView.userInteractionEnabled = YES;
//    self.reusedImageView.userInteractionEnabled = YES;
//    
//    // 监听图片点击事件
//    // 1.0 添加手势到控件
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
//    
////    [self.reusedImageView addGestureRecognizer:tap];
//    [self.centerImageView addGestureRecognizer:tap];
    
    
    
}

- (void)tap {

    NSLog(@"点击了%zd图片", self.pageView.currentPage);
}
#pragma mark -监听scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    [self startTimer];
}
#pragma mark -实现通知方法
- (void)scroll {

    [self startTimer];

}

- (void)stopScroll {

    [self stopTimer];

}

- (void)startTimer {
    
    // 开启定时器之前,先把之前的定时器从runloop移除
    [self.timer invalidate];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    self.timer = timer;
}
- (void)stopTimer {
 
    [self.timer invalidate];
    self.timer = nil;
}

// 移除通知
- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// 播放下一张
- (void)nextPage {
    
    // 1.0 设置偏移量
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = CGPointMake(imageVW, 0);
        self.imageCount++;
        
    } completion:^(BOOL finished) {
        // 偏移量复原
        self.scrollView.contentOffset = CGPointZero;

        // 2.0 交换图片控件
        CGRect frame = self.centerImageView.frame;
        self.centerImageView.frame = self.reusedImageView.frame;
        self.reusedImageView.frame = frame;
        
        UIImageView *imageView = self.centerImageView;
        self.centerImageView = self.reusedImageView;
        self.reusedImageView = imageView;
        
        self.pageView.currentPage = self.imageCount - 1;

        // 设置图片
        if (self.imageCount >self.pictureArr.count - 1) {
            self.imageCount = 0;
        }

        
    [self.reusedImageView sd_setImageWithURL:self.pictureArr[self.imageCount] placeholderImage:nil];
        
    }];
    
}



@end
